#include "settingWin.h"

lv_obj_t *btn_cont_setting_ensure;
lv_obj_t *label_btn_cont_setting_ensure;
lv_obj_t *label_btn_cont_setting_cancel;
lv_obj_t *cont_setting;
lv_obj_t *btn_cont_setting_cancel;
lv_obj_t *cont_settingWin_top;
lv_obj_t *cont_settingWin_top_label_font24;
lv_style_t style_label_date_setting;
lv_obj_t *label_date_setting;
lv_obj_t *label_tiem_setting;




void btn_cont_setting_ensure_handle(lv_obj_t *obj, lv_event_t event)
{
	if(keyBord_target != 0)return;
	if(obj == btn_cont_setting_ensure && event == LV_EVENT_CLICKED)
	{
		beep_sound_tiems += BUTTON_BEEP_SOUND;
		sDate.Year =  atoi(lv_label_get_text(label_btn_year)) - 2000;
		sDate.Month =  atoi(lv_label_get_text(label_btn_month));
		sDate.Date =  atoi(lv_label_get_text(label_btn_day));
		if(sDate.Month > 12)
		{
			sDate.Month = 12;
		}

		if(sDate.Date > 31)
		{
			sDate.Date = 31;
		}

		if(sDate.Year > 100)
		{
			sDate.Year = 100;
		}


		sTime.Hours =  atoi(lv_label_get_text(label_btn_hour));
		sTime.Minutes =  atoi(lv_label_get_text(label_btn_minute));
		sTime.Seconds =  atoi(lv_label_get_text(label_btn_second));
		sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
		sTime.StoreOperation = RTC_STOREOPERATION_RESET;

		if(sTime.Hours > 24)
		{
			sTime.Hours = 24;
		}

		if(sTime.Minutes > 59)
		{
			sTime.Minutes = 59;
		}
		
				if(sTime.Seconds > 59)
		{
			sTime.Seconds = 59;
		}
		
		
		
	if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
  {
    Error_Handler();
  }

  if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
  {
    Error_Handler();
  }
		
		lv_obj_del(cont_setting);
		return;
	}else if(obj == btn_cont_setting_cancel && event == LV_EVENT_RELEASED)
	{
		beep_sound_tiems += BUTTON_BEEP_SOUND;
		lv_obj_del(cont_setting);
		return;
	
	}

}

lv_obj_t *btn_year;
lv_obj_t *btn_month;
lv_obj_t *btn_day;

lv_obj_t *label_btn_year;
lv_obj_t *label_btn_month;
lv_obj_t *label_btn_day;

lv_obj_t *btn_hour;
lv_obj_t *btn_minute;
lv_obj_t *btn_second;

lv_obj_t *label_btn_hour;
lv_obj_t *label_btn_minute;
lv_obj_t *label_btn_second;




void btn_cont_setting_handle(lv_obj_t *obj,lv_event_t event)
{
	
	if(event != LV_EVENT_CLICKED)return;
	beep_sound_tiems += BUTTON_BEEP_SOUND;
	if(obj == btn_year)
	{
		keyBord_target = change_year;
		lv_obj_set_hidden(cont_keyBord,false);
		lv_obj_move_foreground(cont_keyBord);
	}else if(obj == btn_month)
	{
		keyBord_target = change_month;
		lv_obj_set_hidden(cont_keyBord,false);
		lv_obj_move_foreground(cont_keyBord);
	}else if(obj == btn_day)
	{
		keyBord_target = change_day;
		lv_obj_set_hidden(cont_keyBord,false);
		lv_obj_move_foreground(cont_keyBord);
	}else if(obj == btn_hour)
	{
		keyBord_target = change_hour;
		lv_obj_set_hidden(cont_keyBord,false);
		lv_obj_move_foreground(cont_keyBord);
	}else if(obj == btn_minute)
	{
		keyBord_target = change_minute;
		lv_obj_set_hidden(cont_keyBord,false);
		lv_obj_move_foreground(cont_keyBord);
	}else if(obj == btn_second)
	{
		keyBord_target = change_second;
		lv_obj_set_hidden(cont_keyBord,false);
		lv_obj_move_foreground(cont_keyBord);
	}
}

void show_cont_setting()
{
	keyBord_target = 0;
	cont_setting = lv_cont_create(main_cont,main_cont);
	lv_obj_set_size(cont_setting,800,480);
	btn_cont_setting_ensure = lv_btn_create(cont_setting, btn_reset);
	label_btn_cont_setting_ensure = lv_label_create(btn_cont_setting_ensure,label_testSetting_1);
	lv_obj_set_event_cb(btn_cont_setting_ensure,btn_cont_setting_ensure_handle);
	//150,50
	lv_obj_set_pos(btn_cont_setting_ensure,482,420);
	btn_cont_setting_cancel = lv_btn_create(cont_setting,btn_cont_setting_ensure);
	label_btn_cont_setting_cancel = lv_label_create(btn_cont_setting_cancel,label_btn_cont_setting_ensure);
	lv_obj_set_pos(btn_cont_setting_cancel,166,420);

	
	cont_settingWin_top = lv_cont_create(cont_setting,cont_main_top);
	cont_settingWin_top_label_font24 = lv_label_create(cont_settingWin_top,cont_main_top_label_font24);
	
	lv_obj_set_pos(cont_settingWin_top_label_font24,10,10);
	
	
	lv_obj_t *cont_data;
	cont_data = lv_cont_create(cont_setting,cont_settingWin_top);
	lv_obj_set_pos(cont_data,25,90);
	lv_obj_set_size(cont_data,340,80);
	label_date_setting = lv_label_create(cont_data,label_testSetting_1);
	
	lv_obj_set_pos(label_date_setting,4,12);
	

	
	lv_obj_t *label_ydt;
	label_ydt = lv_label_create(cont_data,label_date_setting);
	
	lv_obj_set_pos(label_ydt,6,45);
	
	
	btn_year = lv_btn_create(cont_data,btn_testSetting_1);
	lv_obj_set_size(btn_year,80,35);
	lv_obj_set_pos(btn_year,28,40);
	lv_obj_set_event_cb(btn_year,btn_cont_setting_handle);
	
	
	label_btn_year = lv_label_create(btn_year,label_ydt);
	lv_label_set_text_fmt(label_btn_year,"%d",sDate.Year+2000);
	
	
	btn_month = lv_btn_create(cont_data,btn_testSetting_1);
	lv_obj_set_size(btn_month,80,35);
	lv_obj_set_pos(btn_month,130,40);
	lv_obj_set_event_cb(btn_month,btn_cont_setting_handle);
	
	
	label_btn_month = lv_label_create(btn_month,label_ydt);
	lv_label_set_text_fmt(label_btn_month,"%d",sDate.Month);
	
	
	
	btn_day = lv_btn_create(cont_data,btn_testSetting_1);
	lv_obj_set_size(btn_day,80,35);
	lv_obj_set_pos(btn_day,235,40);
	lv_obj_set_event_cb(btn_day,btn_cont_setting_handle);
		
	
	label_btn_day = lv_label_create(btn_day,label_ydt);
	lv_label_set_text_fmt(label_btn_day,"%d",sDate.Date);

	
	
	
	lv_obj_t *cont_time;
	cont_time = lv_cont_create(cont_setting,cont_settingWin_top);
	lv_obj_set_pos(cont_time,25,210);
	lv_obj_set_size(cont_time,340,80);
	label_tiem_setting = lv_label_create(cont_time,label_testSetting_1);
	
	lv_obj_set_pos(label_tiem_setting,4,12);
	

	
	lv_obj_t *label_hms;
	label_hms = lv_label_create(cont_time,label_tiem_setting);
	
	lv_obj_set_pos(label_hms,6,45);
	
	
	btn_hour = lv_btn_create(cont_time,btn_testSetting_1);
	lv_obj_set_size(btn_hour,80,35);
	lv_obj_set_pos(btn_hour,28,40);
	lv_obj_set_event_cb(btn_hour,btn_cont_setting_handle);
	
	
	label_btn_hour = lv_label_create(btn_hour,label_ydt);
	lv_label_set_text_fmt(label_btn_hour,"%d",sTime.Hours);
	
	
	btn_minute = lv_btn_create(cont_time,btn_testSetting_1);
	lv_obj_set_size(btn_minute,80,35);
	lv_obj_set_pos(btn_minute,130,40);
	lv_obj_set_event_cb(btn_minute,btn_cont_setting_handle);
	
	
	label_btn_minute = lv_label_create(btn_minute,label_ydt);
	lv_label_set_text_fmt(label_btn_minute,"%d",sTime.Minutes);
	
	
	
	btn_second = lv_btn_create(cont_time,btn_testSetting_1);
	lv_obj_set_size(btn_second,80,35);
	lv_obj_set_pos(btn_second,235,40);
	lv_obj_set_event_cb(btn_second,btn_cont_setting_handle);
	

	label_btn_second = lv_label_create(btn_second,label_ydt);
	lv_label_set_text_fmt(label_btn_second,"%d",sTime.Seconds);
	
	
	if(language == 1)
	{
		lv_label_set_text(label_btn_cont_setting_ensure,"确定");
		lv_label_set_text(label_btn_cont_setting_cancel,"取消");
		lv_label_set_text(cont_settingWin_top_label_font24,"时间设置");
		lv_label_set_text(label_date_setting,"日期");
		lv_label_set_text(label_ydt,"年：            月：            日：            ");
		lv_label_set_text(label_tiem_setting,"时间");
		lv_label_set_text(label_hms,"时：            分：            秒：            ");
	}else if(language == 2)
	{
		lv_label_set_text(label_btn_cont_setting_ensure,"Enter");
		lv_label_set_text(label_btn_cont_setting_cancel,"Cancel");
		lv_label_set_text(cont_settingWin_top_label_font24,"Date setting");
		lv_label_set_text(label_date_setting,"Date");
		lv_label_set_text(label_ydt, "Y:                      M:                      D:  ");
		lv_label_set_text(label_tiem_setting,"Time");  
		lv_label_set_text(label_hms, "H:                      M:                      S:  ");
	}	
	
	
//	  lv_obj_t *btn13 = lv_btn_create(cont_setting,btn_testSetting_1);
//    lv_obj_set_click(btn13,false);
//    label_date_setting = lv_label_create(btn13,label_testSetting_1);
//    lv_obj_set_pos(btn13,50,80);
//    lv_obj_set_size(btn13,100,35);
//		
//		
//		lv_obj_t *btn14 = lv_btn_create(cont_setting,btn13);
//    lv_obj_set_click(btn14,false);
//		label_tiem_setting = lv_label_create(btn14,label_testSetting_1);
//    lv_obj_set_pos(btn14,50,160);
//    lv_obj_set_size(btn14,100,35);

//	label_tiem_setting = lv_label_create(cont_setting,label_testSetting_1);
//	lv_style_copy(&style_label_date_setting,lv_obj_get_style(label_date_setting));

//	lv_label_set_text(label_tiem_setting,"时间");
//	style_label_date_setting.body.border.width = 3;
//	style_label_date_setting.body.border.color = LV_COLOR_BLACK;
//	lv_label_set_style(label_date_setting,LV_LABEL_STYLE_MAIN,&style_label_date_setting);
//	lv_obj_set_pos(label_date_setting,50,80);
//	lv_obj_set_pos(label_tiem_setting,50,140);
	
	
	
	
}

